﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Utils;

namespace QLSX.BO
{
    class ThanhPhan
    {
        #region Property

        public int Stt { set; get; }
        public int ThanhPhanId { set; get; }
        public string TenThanhPhan { set; get; }
        public double GiaTriMacDinh { set; get; }
        public string DonVi { set; get; }
        public string MoTa { set; get; }

        #endregion

        public static List<ThanhPhan> SelectAllThanhPhan()
        {
            SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            DataTable table = new DataTable();
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SelectAllThanhPhan", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adt = new SqlDataAdapter(cmd);
                adt.Fill(table);

                if (table == null || table.Rows.Count == 0)
                {
                    return null;
                }
                List<ThanhPhan> listThanhPhan = new List<ThanhPhan>();
                int counter = 1;
                foreach (DataRow row in table.Rows)
                {
                    ThanhPhan tp = new ThanhPhan();
                    tp.Stt = counter++;
                    tp.ThanhPhanId = Int32.Parse(row["ThanhPhanId"].ToString());
                    tp.TenThanhPhan = row["TenThanhPhan"].ToString();
                    tp.GiaTriMacDinh = Int32.Parse(row["GiaTriMacDinh"].ToString());
                    tp.DonVi = row["DonVi"].ToString();
                    tp.MoTa = row["MoTa"].ToString();
                    listThanhPhan.Add(tp);
                }
                return listThanhPhan;
            }
            catch
            {
                table.Dispose();
                return null;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }

        public static ThanhPhan SelectThanhPhanById(int ThanhPhanId)
        {
            SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SelectThanhPhanByThanhPhanId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ThanhPhanId", ThanhPhanId);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    ThanhPhan tp = new ThanhPhan();
                    tp.TenThanhPhan = dr["TenThanhPhan"].ToString();
                    tp.GiaTriMacDinh = Double.Parse(dr["GiaTriMacDinh"].ToString());
                    tp.DonVi = dr["DonVi"].ToString();
                    tp.MoTa = dr["MoTa"].ToString();
                    return tp;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }

        public static ThanhPhan SelectThanhPhanByName(string TenThanhPhan)
        {
            SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SelectThanhPhanByTen", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TenThanhPhan", TenThanhPhan);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    ThanhPhan tp = new ThanhPhan();
                    tp.TenThanhPhan = dr["TenThanhPhan"].ToString();
                    tp.GiaTriMacDinh = Int32.Parse(dr["GiaTriMacDinh"].ToString());
                    tp.DonVi = dr["DonVi"].ToString();
                    tp.MoTa = dr["MoTa"].ToString();
                    return tp;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }
        public static bool AddThanhPhan(ThanhPhan tp)
        {
            //SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            //try
            //{
            //    con.Open();
            //    SqlCommand cmd = new SqlCommand("InsertThanhPhan", con);
            //    cmd.CommandType = CommandType.StoredProcedure;
            //    cmd.Parameters.AddWithValue("@TenThanhPhan", tp.TenThanhPhan);
            //    cmd.Parameters.AddWithValue("@SoLuong", tp.SoLuong);
            //    cmd.Parameters.AddWithValue("@SoLuongThucTe", tp.SoLuongThucTe);
            //    cmd.Parameters.AddWithValue("@NgaySanXuat", tp.NgaySanXuat);
            //    cmd.Parameters.AddWithValue("@NgayCapNhatSoLuong", tp.NgayCapNhatSoLuong);
            //    // add parameters
            //    SqlParameter outId = new SqlParameter("@NewThanhPhanId", SqlDbType.Int, int.MaxValue);
            //    outId.Direction = ParameterDirection.Output;
            //    cmd.Parameters.Add(outId);
            //    cmd.ExecuteNonQuery();
            //    tp.ThanhPhanId = (int)cmd.Parameters["@NewThanhPhanId"].Value;
            //    return true; // success 
            //}
            //catch (Exception ex)
            //{
            //    return false;  // return error message
            //}
            //finally
            //{
            //    con.Close();
            //}

            return false;
        }
        public static bool DeleteThanhPhan(int ThanhPhanId)
        {
            SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("DeleteThanhPhan", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ThanhPhanId", ThanhPhanId);
                cmd.ExecuteNonQuery();
                return true; // success 
            }
            catch (Exception ex)
            {
                return false;  // return error message
            }
            finally
            {
                con.Close();
            }
        }
        public static bool UpdateThanhPhan(ThanhPhan tp)
        {
            //SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            //try
            //{
            //    con.Open();
            //    SqlCommand cmd = new SqlCommand("UpdateThanhPhan", con);
            //    cmd.CommandType = CommandType.StoredProcedure;
            //    cmd.Parameters.AddWithValue("@ThanhPhanId", tp.ThanhPhanId);
            //    cmd.Parameters.AddWithValue("@TenThanhPhan", tp.TenThanhPhan);
            //    cmd.Parameters.AddWithValue("@SoLuong", tp.SoLuong);
            //    cmd.Parameters.AddWithValue("@SoLuongThucTe", tp.SoLuongThucTe);
            //    cmd.Parameters.AddWithValue("@NgaySanXuat", tp.NgaySanXuat);
            //    cmd.Parameters.AddWithValue("@NgayCapNhatSoLuong", tp.NgayCapNhatSoLuong);
            //    cmd.ExecuteNonQuery();
            //    return true; // success 
            //}
            //catch (Exception ex)
            //{
            //    return false;  // return error message
            //}
            //finally
            //{
            //    con.Close();
            //}

            return false;
        }

        public static bool UpdateThanhPhan_GiaTriMacDinh(int ThanhPhanId, double giaTriMacDinh)
        {
            SqlConnection con = new SqlConnection(ApplicationConfig.GetConnectionString());
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UpdateThanhPhan_GiaTriMacDinh", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ThanhPhanId", ThanhPhanId);
                cmd.Parameters.AddWithValue("@GiaTriMacDinh", giaTriMacDinh);
                cmd.ExecuteNonQuery();
                return true; // success 
            }
            catch (Exception ex)
            {
                return false;  // return error message
            }
            finally
            {
                con.Close();
            }
            return false;
        }
    }
}
